package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjUserType;
import com.zb.extend.UserTypeKnowledgeTypeExtend;
import com.zb.mapper.ZjUserTypeMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjUserService;
import com.zb.service.ZjUserTypeKnowledgeTypeService;
import com.zb.service.ZjUserTypeService;
import com.zb.service.ZjUserTypeThirdService;
import com.zb.vo.UserTypeListVo;
import com.zb.vo.UserTypeSelectVo;
import com.zb.vo.UserTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户分类表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-08
 */
@Service
public class ZjUserTypeServiceImpl extends ServiceImpl<ZjUserTypeMapper, ZjUserType> implements ZjUserTypeService {
    @Resource
    private ZjUserTypeMapper userTypeMapper;
    @Resource
    private ZjUserTypeThirdService typeThirdService;
    @Resource
    private ZjUserService userService;
    @Resource
    private ZjUserTypeKnowledgeTypeService userTypeKnowledgeTypeService;

    // 新增或修改用户分类
    @Override
    public void save(Long custAccId, UserTypeVo typeVo) {
        Assert.isTrue(null == userTypeMapper.existByName(custAccId, typeVo.getId(), typeVo.getName()), "分类名称已重复！", typeVo.getName());
        ZjUserType type = new ZjUserType();
        BeanUtils.copyProperties(typeVo, type);
        type.setDisabled(typeVo.getStatus());
        if (null != type.getId()) {
            this.updateById(type);
        } else {
            type.setId(IdWorker.getId());
            type.setCustAccId(custAccId);
            this.save(type);
        }
    }

    // 获取用户分类列表
    @Override
    public Map<String, Object> getList(Long custAccId, UserTypeSelectVo selectVo) {
        QueryWrapper<ZjUserType> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (null != selectVo.getStatus()) {
            wrapper.eq("is_disabled", selectVo.getStatus());
        }
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjUserType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<Long> userTypeIds = page.getRecords().stream().map(ZjUserType::getId).toList();

        List<UserTypeKnowledgeTypeExtend> typeExtendList = userTypeKnowledgeTypeService.getByUserTypeIds(userTypeIds);

        List<UserTypeListVo> voList = page.getRecords().stream().map(t -> {
            UserTypeListVo vo = UserTypeListVo.beanToVo(t);
            vo.setKnowledgeType(typeExtendList.stream().filter(f -> Objects.equals(f.getUserTypeId(), t.getId())).map(UserTypeKnowledgeTypeExtend::getKnowledgeType).collect(Collectors.joining("、")));
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 批量更改用户分类状态
    @Override
    public void updateBatchDisabledByIds(List<Long> idList, boolean status) {
        userTypeMapper.updateBatchDisabledByIds(idList, status);
    }

    // 批量删除用户分类
    @Override
    public void deleteBatchByIds(List<Long> idList) {
        List<String> userTypes = userService.existTypeByTypeIds(idList);
        Assert.isTrue(null == userTypes || userTypes.isEmpty(), "用户类型[" + String.join("、", userTypes) + "]已关联用户，不可删除", null);
        typeThirdService.removeByIds(idList);
        this.removeByIds(idList);
    }

    // 分页获取用户分类下拉数据列表
    @Override
    public Map<String, Object> pageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjUserType> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId)
                .eq("is_disabled", false)
                .orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjUserType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(new SelectResultVo(t.getId(), t.getName())));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据ID查询信息
    @Override
    public List<String> getNameByIds(List<Long> ids) {
        if (null == ids || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjUserType> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "is_disabled AS disabled");
        wrapper.in("id", ids);
        wrapper.last(" LIMIT 10");
        return this.list(wrapper).stream().map(ZjUserType::getName).toList();
    }
}
